package com.zg.autoflow.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.zg.autoflow.req.AutoflowInherentFieldsAddInfoReq;
import com.zg.autoflow.req.AutoflowInherentFieldsAddReq;
import com.zg.autoflow.service.AutoflowInherentFieldsService;
import com.zg.common.core.dao.Inherent.AutoflowInherentFields;
import com.zg.common.core.dao.Inherent.dao.AutoflowInherentFieldsDao;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author zg
*/
@Service
@RequiredArgsConstructor
public class AutoflowInherentFieldsServiceImpl implements AutoflowInherentFieldsService {

    private final AutoflowInherentFieldsDao autoflowInherentFieldsDao;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addOrUpdate(AutoflowInherentFieldsAddInfoReq req) {
        Long inherentId = req.getInherentId();
        List<AutoflowInherentFieldsAddReq> fields = req.getFields();
        fields.forEach(line->{
            //创建对象
            AutoflowInherentFields obj = new AutoflowInherentFields();
            //属性处理
            BeanUtil.copyProperties(line, obj);
            obj.setInherentId(inherentId);
            StringBuffer strBuff = new StringBuffer();
            obj.setSearchStr(strBuff.toString());
            //保存对象
            if (ObjectUtil.isEmpty(obj.getId())){
                autoflowInherentFieldsDao.save(obj);
            }else {
                autoflowInherentFieldsDao.updateById(obj);
            }
        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del(List<Long> idList) {
        autoflowInherentFieldsDao.removeBatchByIds(idList);
    }

}
